package com.seo.service.proxy.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.seo.mapper.proxy.ProxyUserMapper;
import com.seo.pojo.ReturnBody;
import com.seo.service.proxy.sdk.ProxyUserService;

import cn.hutool.crypto.SecureUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("proxyUserService")
public class ProxyUserServiceImpl implements ProxyUserService {

	@Autowired
	private ProxyUserMapper proxyUserMapper;

	@Transactional(readOnly = true)
	@Override
	public ReturnBody login(Integer userId, String userName, String password, String code) {
		ReturnBody rb = ReturnBody.init();
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId);
		params.put("userName", userName);
		Map<String, Object> proxyUser = proxyUserMapper.getProxyUserByUserName(params);
		if (proxyUser == null) {
			return rb.error("error", "用户不存在");
		}
		String state = (String) proxyUser.get("state");
		if (!"01".equals(state)) {
			return rb.error("error", "用户被禁用");
		}

		String realPassword = (String) proxyUser.get("password");

		String inputPassword = SecureUtil.md5(password);
		if (!inputPassword.equals(realPassword)) {
			return rb.error("error", "输入密码错误");
		}
		rb.put("proxyUser", proxyUser);

		return rb.success();
	}

	
	
	
	@Override
	public ReturnBody getProxyUserByName(Integer userId, String userName) {
		ReturnBody rb = ReturnBody.init();
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId);
		params.put("userName", userName);
		Map<String, Object> proxyUser = proxyUserMapper.getProxyUserByUserName(params);
		if (proxyUser == null) {
			return rb.error("error", "用户不存在");
		}
		String state = (String) proxyUser.get("state");
		if (!"01".equals(state)) {
			return rb.error("error", "用户被禁用");
		}
		rb.put("proxyUser", proxyUser);
		rb.put("proxyUserId", proxyUser.get("id"));
		return rb.success();
	}




	@Override
	public List<Map<String, Object>> getProxyUserList(Map<String, Object> params) {
		return proxyUserMapper.getProxyUserList(params);
	}

	@Override
	public Map<String, Object> getProxyUserById(Integer id) {
		return proxyUserMapper.getProxyUserById(id);
	}

	@Override
	public Integer getProxyUserCnt(Map<String, Object> params) {
		return proxyUserMapper.getProxyUserCnt(params);
	}

	@Override
	public ReturnBody addProxyUser(Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		try {
			if (!params.containsKey("userId")) {
				return rb.error("代理用户不能为空");
			}
			Integer userId = (Integer) params.get("userId");
			if (userId == null || userId <= 0) {
				return rb.error("代理用户不能为空");
			}

			if (!params.containsKey("userName")) {
				return rb.error("请输入账号");
			}
			String userName = (String) params.get("userName");
			if (StringUtils.isEmpty(userName)) {
				return rb.error("请输入账号");
			}

			if (!params.containsKey("petName")) {
				return rb.error("请输入昵称");
			}
			String petName = (String) params.get("petName");
			if (StringUtils.isEmpty(petName)) {
				return rb.error("请输入昵称");
			}

			if (!params.containsKey("password")) {
				return rb.error("请输入密码");
			}
			String password = (String) params.get("password");
			if (StringUtils.isEmpty(password)) {
				return rb.error("请输入密码");
			}

			if (!params.containsKey("phone")) {
				return rb.error("请输入手机号");
			}
			String phone = (String) params.get("phone");
			if (StringUtils.isEmpty(phone)) {
				return rb.error("请输入手机号");
			}

			if (!params.containsKey("email")) {
				return rb.error("请输入邮箱");
			}
			String email = (String) params.get("email");
			if (StringUtils.isEmpty(email)) {
				return rb.error("请输入邮箱");
			}
			
			if (!params.containsKey("creditValue")) {
				params.put("creditValue", 0);
			}
			
			params.put("password", SecureUtil.md5(password));
			params.put("state", "01");

			int num = proxyUserMapper.addProxyUser(params);
			if (num == 0) {
				return rb.rollback();
			}
			return rb.success();
		} catch (Exception e) {
			log.error("异常-{}", e);
			return rb.rollback();
		} finally {
			rb.result();
		}

	}

	@Override
	public ReturnBody deleteProxyUser(Integer id) {
		ReturnBody rb = ReturnBody.init();
		int num = proxyUserMapper.deleteProxyUser(id);
		if (num == 0) {
			return rb.error();
		}

		return rb.success();
	}

	@Override
	public ReturnBody updateProxyUser(Map<String, Object> params) {

		ReturnBody rb = ReturnBody.init();
		try {
			if (!params.containsKey("id")) {
				return rb.error("用户不存在");
			}
			String id = (String) params.get("id");
			if (StringUtils.isEmpty(id)) {
				return rb.error("用户不存在");
			}

			if (params.containsKey("userId")) {

				Integer userId = (Integer) params.get("userId");
				if (userId == null || userId <= 0) {
					return rb.error("代理用户不能为空");
				}
			}
			if (params.containsKey("petName")) {

				String petName = (String) params.get("petName");
				if (StringUtils.isEmpty(petName)) {
					return rb.error("请输入昵称");
				}
			}
			if (params.containsKey("phone")) {

				String phone = (String) params.get("phone");
				if (StringUtils.isEmpty(phone)) {
					return rb.error("请输入手机号");
				}
			}
			if (params.containsKey("email")) {

				String email = (String) params.get("email");
				if (StringUtils.isEmpty(email)) {
					return rb.error("请输入邮箱");
				}
			}
			
			if (!params.containsKey("creditValue")) {
				params.put("creditValue", 0);
			}
			
			if (params.containsKey("sex")) {

				String sex = (String) params.get("sex");
				if (StringUtils.isEmpty(sex)) {
					return rb.error("请选择性别");
				}
			}

			int num = proxyUserMapper.updateProxyUser(params);
			if (num == 0) {
				return rb.rollback();
			}
			return rb.success();
		} catch (Exception e) {
			log.error("异常-{}", e);
			return rb.rollback();
		} finally {
			rb.result();
		}

	}

	@Override
	public ReturnBody updateProxyUserState(Integer id, String state) {

		ReturnBody rb = ReturnBody.init();
		int num = proxyUserMapper.updateProxyUserState(id, state);
		if (num == 0) {
			return rb.error();
		}

		return rb.success();
	}

	@Override
	public ReturnBody updateProxyUserPwd(Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		if (!params.containsKey("oldPassword")) {
			return rb.error("请输入原始密码");
		}

		String oldPassword = (String) params.get("oldPassword");
		if (StringUtils.isEmpty(oldPassword)) {
			return rb.error("请输入原始密码");
		}

		if (!params.containsKey("password")) {
			return rb.error("请输入密码");
		}

		String password = (String) params.get("password");
		if (StringUtils.isEmpty(password)) {
			return rb.error("请输入密码");
		}

		if (!params.containsKey("confirmPassword")) {
			return rb.error("请再次确认密码");
		}

		String confirmPassword = (String) params.get("confirmPassword");
		if (StringUtils.isEmpty(confirmPassword)) {
			return rb.error("请再次确认密码");
		}

		if (!StringUtils.equals(confirmPassword, password)) {
			return rb.error("再次确认密码错误");
		}

		Integer id = (Integer) params.get("id");
		Map<String, Object> user = proxyUserMapper.getProxyUserById(id);
		if (user == null) {
			return rb.error("用户不存在");
		}
		String dataPassword = (String) user.get("password");
		if (!dataPassword.equals(SecureUtil.md5(oldPassword))) {
			return rb.error("原始密码不正确");
		}

		params.put("password", SecureUtil.md5(password));

		int num = proxyUserMapper.updateProxyUserPwd(params);
		if (num == 0) {
			return rb.error("重置密码失败");
		}
		return rb.success();
	}




	@Override
	public ReturnBody updateResetProxyUserPwd(Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		
		if (!params.containsKey("password")) {
			return rb.error("请输入密码");
		}

		String password = (String) params.get("password");
		if (StringUtils.isEmpty(password)) {
			return rb.error("请输入密码");
		}
		Integer id = (Integer) params.get("id");
		Map<String, Object> user = proxyUserMapper.getProxyUserById(id);
		if (user == null) {
			return rb.error("用户不存在");
		}
		
		params.put("password", SecureUtil.md5(password));

		int num = proxyUserMapper.updateProxyUserPwd(params);
		if (num == 0) {
			return rb.error("重置密码失败");
		}
		return rb.success();
	}
	
	

}
